package com.hsh.member.base;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * @Author
 * @date2022/12/1 11:12
 * 封装统一的响应结果
 */
@Data
public class Result implements Serializable {

  private static final Logger logger = LoggerFactory.getLogger(Result.class);
  private Integer code;
  private Boolean flag;
  private String message;
  private Object  data;

  public Result(Integer code, String message, Object data) {
    this.code = code;
    this.message = message;
    this.data = data;
    this.flag = code == ResultEnum.SUCCESS.getCode() ? true: false;
  }

  public static Result ok() {
    return new Result(ResultEnum.SUCCESS.getCode(),  ResultEnum.SUCCESS.getDesc(), null);
  }

  public static Result ok(Object data) {
    return new Result(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getDesc(), data);
  }

  public static Result ok(String message, Object data) {
    return new Result(ResultEnum.SUCCESS.getCode(), message, data);
  }

  public static Result error(String message) {
    logger.debug("返回错误：code={}, message={}", ResultEnum.ERROR.getCode(), message);
    return new Result(ResultEnum.ERROR.getCode(), message, null);
  }

  public static Result build(int code, String message) {
    logger.debug("返回结果：code={}, message={}", code, message);
    return new Result(code, message, null);
  }

  public static Result build(ResultEnum resultEnum) {
    logger.debug("返回结果：code={}, message={}", resultEnum.getCode(), resultEnum.getDesc());
    return new Result(resultEnum.getCode(),  resultEnum.getDesc(), null);
  }

  public String toString() {
    return JSON.toJSONString(this);
  }
}
